<?php
/******************************
 * EQdkp
 * Copyright 2002-2003
 * Licensed under the GNU GPL.  See COPYING for full terms.
 * ------------------
 * viewitemrequest.php
 * Began: November 2007
 *
 *
 ******************************/

define('EQDKP_INC', true);
$eqdkp_root_path = './';
include_once($eqdkp_root_path . 'common.php');

$user->check_auth('u_item_view');

if ( (isset($_GET[URI_ITEM])) && (intval($_GET[URI_ITEM] > 0)) )
{
    $sort_order = array(
        0 => array('item_requester', 'item_requester desc'),
	1 => array('request_fulfilled', 'request_fulfilled desc'),
	2 => array('member_points', 'member_points desc'),
	3 => array('thirty_count desc', 'thirty_count'),
	4 => array('sixty_count desc', 'sixty_count'),
	5 => array('ninety_count desc', 'ninety_count'),
	6 => array('lifetime_percent desc', 'lifetime_percent')
    );

    $current_order = switch_order($sort_order);

    // We want to view items by name and not id, so get the name
    $item_name = $db->query_first('SELECT item_name FROM ' . ITEM_REQUEST_TABLE . " WHERE item_id='".$_GET[URI_ITEM]."'");

    if ( empty($item_name) )
    {
        message_die($user->lang['error_invalid_item_provided']);
    }

    // Item stats
    $show_stats = true;

    $sql = 'SELECT item_data_id
            FROM ' . ITEM_DATA_TABLE . "
            WHERE item_name='" . addslashes($item_name) . "'";
    $stat_id = $db->query_first($sql);
    if ( !$stat_id )
    {
        $show_stats = false;
        $u_view_stats = '';
    }
    else
    {
        $u_view_stats = $eqdkp_root_path . 'viewitemdata.php'.$SID.'&amp;'.URI_ITEM.'='.$stat_id.'&amp;iframe=true';
    }

    if ( $_GET[URI_PAGE] == 'history' )
    {
	$sql = 'SELECT item_id, item_name, item_requester, request_fulfilled, ((member_earned - member_spent) + member_adjustment) as member_points,
		       vmdt.raid_count AS thirty_count, vmdt.raid_percent*100 AS thirty_percent,
		       vmds.raid_count AS sixty_count, vmds.raid_percent*100 AS sixty_percent,
		       vmdn.raid_count AS ninety_count, vmdn.raid_percent*100 AS ninety_percent,
		       vmdl.raid_count AS lifetime_count, vmdl.raid_percent*100 AS lifetime_percent
		FROM ' . ITEM_REQUEST_TABLE . ", " .MEMBERS_TABLE. " m
        	LEFT JOIN ".MEMBER_THIRTY_VIEW." vmdt
        	       ON (m.member_id = vmdt.member_id)
       		LEFT JOIN ".MEMBER_SIXTY_VIEW." vmds
        	       ON (m.member_id = vmds.member_id)
        	LEFT JOIN ".MEMBER_NINETY_VIEW." vmdn
        	       ON (m.member_id = vmdn.member_id)
        	LEFT JOIN ".MEMBER_LIFETIME_VIEW." vmdl
        	       ON (m.member_id = vmdl.member_id)
		WHERE item_name='".addslashes($item_name)."'
		  AND ".ITEM_REQUEST_TABLE.".item_requester = m.member_name
		ORDER BY ".$current_order['sql'];
    }
    else
    {
	$sql = 'SELECT item_id, item_name, item_requester, request_fulfilled, ((member_earned - member_spent) + member_adjustment) as member_points,
                       vmdt.raid_count AS thirty_count, vmdt.raid_percent*100 AS thirty_percent,
                       vmds.raid_count AS sixty_count, vmds.raid_percent*100 AS sixty_percent,
                       vmdn.raid_count AS ninety_count, vmdn.raid_percent*100 AS ninety_percent,
                       vmdl.raid_count AS lifetime_count, vmdl.raid_percent*100 AS lifetime_percent
	    	FROM ' . ITEM_REQUEST_TABLE . ", " .MEMBERS_TABLE. " m
                LEFT JOIN ".MEMBER_THIRTY_VIEW." vmdt
                       ON (m.member_id = vmdt.member_id)
                LEFT JOIN ".MEMBER_SIXTY_VIEW." vmds
                       ON (m.member_id = vmds.member_id)
                LEFT JOIN ".MEMBER_NINETY_VIEW." vmdn
                       ON (m.member_id = vmdn.member_id)
                LEFT JOIN ".MEMBER_LIFETIME_VIEW." vmdl
                       ON (m.member_id = vmdl.member_id)
	    	WHERE item_name='".addslashes($item_name)."'
		  AND ".ITEM_REQUEST_TABLE.".item_requester = m.member_name
		  AND request_fulfilled = 0
	    	ORDER BY ".$current_order['sql'];
    }

    if ( !($items_result = $db->query($sql)) )
    {
        message_die('Could not obtain item information', '', __FILE__, __LINE__, $sql);
    }
    while ( $item = $db->fetch_record($items_result) )
    {
        $tpl->assign_block_vars('items_row', array(
            'ROW_CLASS' => $eqdkp->switch_row_class(),
            'REQUESTER' => ( !empty($item['item_requester']) ) ? $item['item_requester'] : '&nbsp;',
            'U_VIEW_REQUESTER' => 'viewmember.php'.$SID.'&amp;' . URI_NAME . '='.$item['item_requester'],
	    'ITEM' => $item_name,
	    'MEMBER_CLASS_STRING' => ($item['member_points'] > 0) ? 'positive' : 'negative',
	    'MEMBER_POINTS' => $item['member_points'],
	    'THIRTY_COUNT' => ( !empty($item['thirty_count']) ) ? $item['thirty_count'] : '0',
	    'SIXTY_COUNT' => ( !empty($item['sixty_count']) ) ? $item['sixty_count'] : '0',
	    'NINETY_COUNT' => ( !empty($item['ninety_count']) ) ? $item['ninety_count'] : '0',
	    'LIFETIME_COUNT' => ( !empty($item['lifetime_count']) ) ? $item['lifetime_count'] : '0',
	    'C_THIRTY_PERCENT' => color_item($item['thirty_percent'], true),
	    'THIRTY_PERCENT' => ( !empty($item['thirty_percent']) ) ? round($item['thirty_percent']) : '0',
	    'C_SIXTY_PERCENT' => color_item($item['sixty_percent'], true),
	    'SIXTY_PERCENT' => ( !empty($item['sixty_percent']) ) ? round($item['sixty_percent']) : '0',
	    'C_NINETY_PERCENT' => color_item($item['ninety_percent'], true),
	    'NINETY_PERCENT' => ( !empty($item['ninety_percent']) ) ? round($item['ninety_percent']) : '0',
	    'C_LIFETIME_PERCENT' => color_item($item['lifetime_percent'], true),
	    'LIFETIME_PERCENT' => ( !empty($item['lifetime_percent']) ) ? round($item['lifetime_percent']) : '0',
	    'REQ_FULFILLED' => ( $item['request_fulfilled'] == 1 ) ? 'Yes' : 'No')
        );
    }

    $tpl->assign_vars(array(
        'S_STATS' => $show_stats,

        'L_PURCHASE_HISTORY_FOR' => 'Request History for '. stripslashes($item_name),
        'L_REQUESTER' => $user->lang['requester'],
	'L_THIRTY' => $user->lang['30_day'],
	'L_SIXTY' => $user->lang['60_day'],
	'L_NINETY' => $user->lang['90_day'],
	'L_LIFETIME' => $user->lang['lifetime'],
	'L_ITEM' => $user->lang['item'],
	'L_REQ_FULFILLED' => $user->lang['req_fulfilled'],
	'L_MEMBER_POINTS' => $user->lang['member_points'],

        'O_REQUESTER' => $current_order['uri'][0],
	'O_REQ_FULFILLED' => $current_order['uri'][1],
	'O_MEMBER_POINTS' => $current_order['uri'][2],
	'O_THIRTY' => $current_order['uri'][3],
	'O_SIXTY' => $current_order['uri'][4],
	'O_NINETY' => $current_order['uri'][5],
	'O_LIFETIME' => $current_order['uri'][6],

        'U_VIEW_STATS' => $u_view_stats,
        'U_VIEW_ITEM' => 'viewitemrequest.php'.$SID.'&amp;' . URI_ITEM . '='.$_GET[URI_ITEM].'&amp;'.URI_PAGE.'='.$_GET[URI_PAGE].'&amp;',

        'VIEWITEM_FOOTCOUNT' => sprintf($user->lang['viewitem_footcount'], $db->num_rows($items_result)))
    );

    $pm->do_hooks('/viewitem.php');

    $eqdkp->set_vars(array(
        'page_title'    => sprintf($user->lang['title_prefix'], $eqdkp->config['guildtag'], $eqdkp->config['dkp_name']).': Viewing Request History for '.stripslashes($item_name),
        'template_file' => 'viewitemrequest.html',
        'display'       => true)
    );
}
else
{
    message_die($user->lang['error_invalid_item_provided']);
}
?>
